﻿using NewRen.Models.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace NewRen.Areas.Mobile.Controllers
{
    [RouteArea("Mobile")]
    public class UserController : BaseController
    {
        // GET: Mobile/User
        public ActionResult Index()
        {
            return View();
        }

        [Route("Show")]
        public ActionResult Show()
        {
            if (CurrentUser == null)
            {
                return Redirect("/Mobile/Login");
            }
            var user = CurrentUser;
            vUser _user = new vUser(user);
            return View(_user);
        }

        [HttpGet]
        [Route("Login")]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Route("Login")]
        public ActionResult Login(string Username,string Password)
        {
            if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
            {
                ModelState.AddModelError("", "用户名或者密码不能为空！");
            }
            Password = Helpers.Encryt.GetMD5(Password);
            var user = db.Users.Where(u => u.UserName == Username && u.Password == Password).FirstOrDefault();
            if (user == null)
            {
                ModelState.AddModelError("", "用户名或者密码错误！");
            }
            else
            {
                FormsAuthentication.SetAuthCookie(Username, false);
                return RedirectToAction("Index", "Home");
            }
            return View();
        }
    }
}